function agricola(num,autocrop,demo)
%agricola Spusteni analyzy obrazu
%   Spusti funkce potrebne pro detekovani objektu
%   num - cislo obrazku z adresare data
%   demo 0/1 - vypne/zapne vykreslovani kroku 
%   autocrop 0/1 - zapne/vypne rucni orez

clearvars -except num demo autocrop
close all
if isnumeric(num)
  imorig=imread(['data/snapshot-',num2str(num),'.jpeg']); % Nacteni obrazku
else
  imorig=imread('http://147.228.47.85/snapshot.jpg');
end
if autocrop 
  [imorig]=imcrop(imorig,[400,250, 600,400]);
else
  [imorig]=imcrop(imorig);
end

[board]=findBoard(imorig,demo); % Nalezeni hraci desky
hsvboard=rgb2hsv(board); % Prevod do HSV
blue=hsvboard(:,:,1)>0.5&hsvboard(:,:,2)>0.2&hsvboard(:,:,3)>0.5&board(:,:,3)>160; % Vyber modre barvy
[rect,cntFarma]=findFarms(hsvboard,demo); % Nalezeni farem
[cntClenRodiny,c,r]=findCircles(blue); % Nalezeni kruhu
for i=1:cntClenRodiny
  blue(floor(c(i,2)-r(i)):ceil(c(i,2)+r(i)),floor(c(i,1)-r(i)):ceil(c(i,1)+r(i)))=0; % Smazani kruhu -> zbydou pouze ohrady
end
[board,cntPastvin]=findFences(board,blue,demo); % Nalezeni ohrad

% Vykresleni
figure
imshow(board)
hold on
plotRect(rect);
viscircles(c,r);

% vypocet bodu
bodyPastvina=min(cntPastvin,4);
if bodyPastvina==0
  bodyPastvina=-1;
end

BODY_FARMA = [-1 -1 1 2 3 4 4 4 4 4 4 4 4 4 4];

bodyFarma=BODY_FARMA(cntFarma+1);
bodyClenRodiny=3*cntClenRodiny;

bodyCelkem=bodyPastvina+bodyFarma+bodyClenRodiny;

fprintf('Hrac ziskal celkem %i bodu.\n',bodyCelkem);
fprintf('Za zorana pole: %i\n',bodyFarma);
fprintf('Za cleny rodiny: %i\n',bodyClenRodiny);
fprintf('Za pastviny: %i\n',bodyPastvina);

export_fig('filename',['Vystup/out',num2str(num),'.jpg'])


